package com.pig4cloud.pig.answerresult.controller;

import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.pig4cloud.pig.common.core.util.R;
import com.pig4cloud.pig.common.log.annotation.SysLog;
import com.pig4cloud.plugin.excel.annotation.ResponseExcel;
import com.pig4cloud.plugin.excel.annotation.RequestExcel;
import com.pig4cloud.pig.answerresult.entity.AnswerResultEntity;
import com.pig4cloud.pig.answerresult.service.AnswerResultService;

import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import com.pig4cloud.pig.common.security.annotation.HasPermission;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.http.HttpHeaders;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;

/**
 * 答题结果表
 *
 * @author pig
 * @date 2025-04-12 15:33:23
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/answerresult" )
@Tag(description = "answerresult" , name = "答题结果表管理" )
@SecurityRequirement(name = HttpHeaders.AUTHORIZATION)
public class AnswerResultController {

    private final  AnswerResultService answerResultService;

    /**
     * 分页查询
     * @param page 分页对象
     * @param answerResult 答题结果表
     * @return
     */
    @Operation(summary = "分页查询" , description = "分页查询" )
    @GetMapping("/page" )
    @HasPermission("answerresult_answerresult_view")
    public R getAnswerResultPage(@ParameterObject Page page, @ParameterObject AnswerResultEntity answerResult) {
        LambdaQueryWrapper<AnswerResultEntity> wrapper = Wrappers.lambdaQuery();
        return R.ok(answerResultService.page(page, wrapper));
    }


    /**
     * 通过条件查询答题结果表
     * @param answerResult 查询条件
     * @return R  对象列表
     */
    @Operation(summary = "通过条件查询" , description = "通过条件查询对象" )
    @GetMapping("/details" )
    @HasPermission("answerresult_answerresult_view")
    public R getDetails(@ParameterObject AnswerResultEntity answerResult) {
        return R.ok(answerResultService.list(Wrappers.query(answerResult)));
    }

    /**
     * 新增答题结果表
     * @param answerResult 答题结果表
     * @return R
     */
    @Operation(summary = "新增答题结果表" , description = "新增答题结果表" )
    @SysLog("新增答题结果表" )
    @PostMapping
    @HasPermission("answerresult_answerresult_add")
    public R save(@RequestBody AnswerResultEntity answerResult) {
        return R.ok(answerResultService.save(answerResult));
    }

    /**
     * 修改答题结果表
     * @param answerResult 答题结果表
     * @return R
     */
    @Operation(summary = "修改答题结果表" , description = "修改答题结果表" )
    @SysLog("修改答题结果表" )
    @PutMapping
    @HasPermission("answerresult_answerresult_edit")
    public R updateById(@RequestBody AnswerResultEntity answerResult) {
        return R.ok(answerResultService.updateById(answerResult));
    }

    /**
     * 通过id删除答题结果表
     * @param ids id列表
     * @return R
     */
    @Operation(summary = "通过id删除答题结果表" , description = "通过id删除答题结果表" )
    @SysLog("通过id删除答题结果表" )
    @DeleteMapping
    @HasPermission("answerresult_answerresult_del")
    public R removeById(@RequestBody String[] ids) {
        return R.ok(answerResultService.removeBatchByIds(CollUtil.toList(ids)));
    }


    /**
     * 导出excel 表格
     * @param answerResult 查询条件
   	 * @param ids 导出指定ID
     * @return excel 文件流
     */
    @ResponseExcel
    @GetMapping("/export")
    @HasPermission("answerresult_answerresult_export")
    public List<AnswerResultEntity> exportExcel(AnswerResultEntity answerResult,String[] ids) {
        return answerResultService.list(Wrappers.lambdaQuery(answerResult).in(ArrayUtil.isNotEmpty(ids), AnswerResultEntity::getId, ids));
    }

    /**
     * 导入excel 表
     * @param answerResultList 对象实体列表
     * @param bindingResult 错误信息列表
     * @return ok fail
     */
    @PostMapping("/import")
    @HasPermission("answerresult_answerresult_export")
    public R importExcel(@RequestExcel List<AnswerResultEntity> answerResultList, BindingResult bindingResult) {
        return R.ok(answerResultService.saveBatch(answerResultList));
    }
}
